package com.senven.api.controller;

import com.senven.api.annotation.LogInfo;
import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.OrganActivity;
import com.senven.api.pojo.OrganActivity;
import com.senven.api.service.OrganActivityService;
import com.senven.api.utils.DateTimeConvert;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author LQP
 */
@RequestMapping("/api/organ/activity")
@Controller
public class OrganActivityController {
    @Autowired
    private OrganActivityService organActivityService;



    /**
     * 获取社团活动
     * @param oid
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganActivityListById")
    @ResponseBody
    public ResponsePage<OrganActivity> queryOrganActivityListById(@RequestParam("oid") int oid,
                                                                  @RequestParam(value = "current",defaultValue = "1") int current,
                                                                  @RequestParam(value = "size", defaultValue = "10") int size) {

        return organActivityService.queryOrganActivityListById(oid,current, size);
    }

    /**
     * 所有人都可以看见的社团活动
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganActivityList")
    @ResponseBody
    public ResponsePage<OrganActivity> queryOrganActivityList(@RequestParam(value = "current",defaultValue = "1") int current,
                                                              @RequestParam(value = "size",defaultValue = "10") int size) {
        return organActivityService.queryOrganActivityList(current, size);
    }

    /**
     * 进行中的社团活动
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganActivityListActive")
    @ResponseBody
    public ResponsePage<OrganActivity> queryOrganActivityListActive(@RequestParam(value = "current",defaultValue = "1") int current,
                                                              @RequestParam(value = "size",defaultValue = "10") int size) {
        return organActivityService.queryOrganActivityListActive(current, size);
    }


    @LogInfo(operateType = "社团业务", operateInfo = "删除活动")
    @GetMapping("/deleteOrganActivityById")
    @ResponseBody
    public ResponseResult deleteOrganActivity(@RequestParam("id")int id){
        int result = organActivityService.deleteOrganActivity(id);
        if (result < 1){
            return ReponseResultUtil.error(-1,"社团活动删除失败");
        }else{
            return ReponseResultUtil.success("社团活动删除成功");
        }
    }

    /**
     * 更新社团社团活动
     * @param organActivity
     * @return
     */
    @LogInfo(operateType = "社团业务", operateInfo = "更新活动")
    @PostMapping("/updateOrganActivity")
    @ResponseBody
    public ResponseResult updateOrganActivity(@RequestBody OrganActivity organActivity){
        int result = organActivityService.updateOrganActivity(organActivity);
        if (result < 1){
            return ReponseResultUtil.error(-1,"社团活动更新失败");
        }else{
            return ReponseResultUtil.success("社团活动更新成功");
        }
    }


    /**
     * 插入社团社团活动
     * @param organActivity
     * @return
     */
    @LogInfo(operateType = "社团业务", operateInfo = "插入活动")
    @PostMapping("/insertOrganActivity")
    @ResponseBody
    public ResponseResult insertOrganActivity(@RequestBody OrganActivity organActivity){
        try {
            ResponsePage activities = organActivityService.queryOrganActivityListByName(organActivity.getName(),1,10);
            if(!activities.getData().isEmpty() || activities.getData().size() != 0){
                return ReponseResultUtil.error(-1, "请勿重复添加");
            }
            int result = organActivityService.insertOrganActivity(organActivity);
            if (result < 1) {
                return ReponseResultUtil.error(-1, "社团活动插入失败");
            } else {
                return ReponseResultUtil.success("社团活动插入成功");
            }
        }catch (Exception e){
            throw  new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    @GetMapping("/getOrganActivityListByName")
    @ResponseBody
    public ResponsePage<OrganActivity> queryOrganActivityListByName(@RequestParam("name") String name,
                                                                    @RequestParam(value = "current",defaultValue = "1") int current,
                                                                    @RequestParam(value = "size",defaultValue = "10")int size){
        return organActivityService.queryOrganActivityListByName(name,current,size);
    }

}
